<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">




<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
  </script>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />



  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico?v=5.1.4">







  <meta name="keywords" content="iOS," />





  <link rel="alternate" href="/atom.xml" title="Swift社区" type="application/atom+xml" />






<meta name="description" content="原文链接：Adding Swift Code as Custom LLDB Command  1. 前言如果问你最常使用哪个 LLDB 命令？ 我相信大多数 iOS 开发者都会回答 po。 你是否知道实际上可以使用纯 Swift 代码自定义自己的 LLDB 命令吗？ 在本文中将会向你展示如何创建你自己的 LLDB 命令。以下是将要分享的内容：  添加你的第一个 LLDB 命令 添加带参数的 LL">
<meta name="keywords" content="iOS">
<meta property="og:type" content="article">
<meta property="og:title" content="如何将 Swift 代码添加为自定义 LLDB 命令">
<meta property="og:url" content="https://fanbaoying.github.io/如何将-Swift-代码添加为自定义-LLDB-命令/index.html">
<meta property="og:site_name" content="Swift社区">
<meta property="og:description" content="原文链接：Adding Swift Code as Custom LLDB Command  1. 前言如果问你最常使用哪个 LLDB 命令？ 我相信大多数 iOS 开发者都会回答 po。 你是否知道实际上可以使用纯 Swift 代码自定义自己的 LLDB 命令吗？ 在本文中将会向你展示如何创建你自己的 LLDB 命令。以下是将要分享的内容：  添加你的第一个 LLDB 命令 添加带参数的 LL">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/2829694-a9a2981a3d603298.gif?imageMogr2/auto-orient/strip">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/2829694-28714c53ef002c05.gif?imageMogr2/auto-orient/strip">
<meta property="og:updated_time" content="2021-06-07T13:09:17.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="如何将 Swift 代码添加为自定义 LLDB 命令">
<meta name="twitter:description" content="原文链接：Adding Swift Code as Custom LLDB Command  1. 前言如果问你最常使用哪个 LLDB 命令？ 我相信大多数 iOS 开发者都会回答 po。 你是否知道实际上可以使用纯 Swift 代码自定义自己的 LLDB 命令吗？ 在本文中将会向你展示如何创建你自己的 LLDB 命令。以下是将要分享的内容：  添加你的第一个 LLDB 命令 添加带参数的 LL">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/2829694-a9a2981a3d603298.gif?imageMogr2/auto-orient/strip">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://fanbaoying.github.io/如何将-Swift-代码添加为自定义-LLDB-命令/"/>





  <title>如何将 Swift 代码添加为自定义 LLDB 命令 | Swift社区</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    
    <a href="https://github.com/SwiftCommunityRes" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Swift社区</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">做最好的 Swift 社区</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://fanbaoying.github.io/如何将-Swift-代码添加为自定义-LLDB-命令/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Swift社区">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars.githubusercontent.com/u/84354365?v=4">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Swift社区">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">如何将 Swift 代码添加为自定义 LLDB 命令</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-06-07T20:59:57+08:00">
                2021-06-07
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Swift-文集/" itemprop="url" rel="index">
                    <span itemprop="name">Swift 文集</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                -->
                <span title="字数统计">
                  数字统计 1,686字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                --> 

                <span title="阅读时长">
                  阅读时长 7分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>原文链接：<a href="https://swiftsenpai.com/testing/add-custom-lldb/" title="Adding Swift Code as Custom LLDB Command" target="_blank" rel="noopener">Adding Swift Code as Custom LLDB Command</a></p>
</blockquote>
<h2 id="1-前言"><a href="#1-前言" class="headerlink" title="1. 前言"></a>1. 前言</h2><p>如果问你最常使用哪个 LLDB 命令？ 我相信大多数 iOS 开发者都会回答 <code>po</code>。 你是否知道实际上可以使用纯 Swift 代码自定义自己的 LLDB 命令吗？</p>
<p>在本文中将会向你展示如何创建你自己的 LLDB 命令。以下是将要分享的内容：</p>
<ul>
<li>添加你的第一个 LLDB 命令</li>
<li>添加带参数的 LLDB 命令</li>
<li>将复杂的 Swift 代码转换为 LLDB 命令</li>
</ul>
<a id="more"></a>
<h2 id="2-添加您的第一个-LLDB-命令"><a href="#2-添加您的第一个-LLDB-命令" class="headerlink" title="2. 添加您的第一个 LLDB 命令"></a>2. 添加您的第一个 LLDB 命令</h2><h3 id="2-1-了解-LLDB-命令结构"><a href="#2-1-了解-LLDB-命令结构" class="headerlink" title="2.1 了解 LLDB 命令结构"></a>2.1 了解 LLDB 命令结构</h3><p>为了添加自定义 LLDB 命令，我们必须利用 <code>command alias</code> LLDB 命令。它结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">command alias [command_name] expr -l Swift -O -- [swift_code]</span><br></pre></td></tr></table></figure>
<p><strong>对命令进行详细分解：</strong></p>
<ul>
<li><code>command alias</code>：使用名称为 Swift 代码添加别名的 LLDB 命令</li>
<li><code>[command_name]</code>: 自定义命令名称</li>
<li><code>expr -l Swift -O --</code>: 要求 LLDB 调试器将后面的所有内容解释为 Swift 代码</li>
<li><code>[swift_code]</code>：定义自定义命令逻辑的 Swift 代码</li>
</ul>
<p>举例说明，如果我们要添加一个别名为 <code>greet</code> 的自定义命令，在控制台上打印结果为 “Hello World！” ，LLDB 命令如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">command alias greet expr -l Swift -O -- print(&quot;Hello World!&quot;)</span><br></pre></td></tr></table></figure>
<h3 id="2-2-添加自定义命令"><a href="#2-2-添加自定义命令" class="headerlink" title="2.2 添加自定义命令"></a>2.2 添加自定义命令</h3><p>现在已经构造了别名为 <code>greet</code> 的命令，然后添加到 LLDB 调试器中。</p>
<p>将 <code>greet</code> 命令添加到 LLDB 调试器的最直接方法是在 Xcode 控制台中执行别名命令。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/2829694-a9a2981a3d603298.gif?imageMogr2/auto-orient/strip" alt=""></p>
<p>但是，这样做只会使 <code>greet</code> 命令在当前特定调试会话中可用。 也就是说，每当开始新的调试会话时，我们就需要重新键入相同的别名命令。</p>
<p>为了避免这种情况发生，我们可以利用位于主目录中的 <code>.lldbinit</code> 文件。 <strong>请注意</strong>，这是一个隐藏文件，如果看不到该文件，可以使用以下快捷方式在你的查找器中显示隐藏文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">shift + command + .</span><br></pre></td></tr></table></figure>
<p>如果在 finder 中启用了显示隐藏文件，仍然没有找到该文件，可以在根目录下使用下面的终端命令创建一个：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">touch ~/.lldbinit</span><br></pre></td></tr></table></figure>
<p>之后，打开 <code>.lldbinit</code> 文件将整个别名命令粘贴到文件中。这样，Xcode 将在每次启动新的调试会话时执行别名命令。</p>
<blockquote>
<p><strong>Pro Tip:</strong> 如果不想在每次更新 <code>.lldbinit</code> 文件时都重新启动调试会话，可以使用以下命令重新加载它：</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">command source ~/.lldbinit</span><br></pre></td></tr></table></figure>
<h2 id="3-添加带参数的-LLDB-命令"><a href="#3-添加带参数的-LLDB-命令" class="headerlink" title="3. 添加带参数的 LLDB 命令"></a>3. 添加带参数的 LLDB 命令</h2><p>接着，让我们尝试添加一个能够接受参数的命令。 在上面 greet 命令的基础上进行修改，使其能够接受一个字符串并且打印出问候消息。</p>
<p>这一次，我们将使用 <code>command regex</code> LLDB 命令。它结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">command regex [command_name] &apos;s/[regex]/expr -l Swift -O -- [swift_code]/&apos;</span><br></pre></td></tr></table></figure>
<p>这里对 regex 命令的工作原理不做详细描述。 通常，是将 [regex] 替换为正则表达式语句 (.+)，然后在 Swift 代码中使用 %1 表示参数。</p>
<p>更新 <code>greet</code>，如下所示：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">command regex greet &apos;s/(.+)/expr -l Swift -O -- print(&quot;Hello \(%1)!&quot;)/&apos;</span><br></pre></td></tr></table></figure>
<p>假设 <code>name = &quot;Swift Senpai&quot;</code> 执行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(lldb) greet name</span><br><span class="line">Hello Swift Senpai!</span><br></pre></td></tr></table></figure>
<p>到这里，你可能会问：如果我需要传入多个参数怎么办？ 答案其实很简单。</p>
<p>首先，将多个 <code>(.+)</code> 添加到正则表达式语句并用空格分隔每个 <code>(.+)</code>。 之后，使用 <code>%2</code>, <code>%3</code>, <code>%4</code>… 来表示 Swift 代码中的每个参数。</p>
<p>将 <code>greet</code> 命令更新为支持两个参数，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">command regex greet &apos;s/(.+) (.+)/expr -l Swift -O -- print(&quot;Hello (%1) and (%2)!&quot;)/&apos;</span><br></pre></td></tr></table></figure>
<p>假设 <code>name1 = &quot;Swift Senpai&quot;</code> 和 <code>name2 = &quot;iOS developers&quot;</code>，使用该命令，结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(lldb) greet name1 name2</span><br><span class="line">Hello Swift Senpai and iOS developers!</span><br></pre></td></tr></table></figure>
<p>到这里，我们已经了解了如何添加带有多个参数的自定义 LLDB 命令。 下面，将向你展示如何将多行 Swift 函数转换为自定义 LLDB 命令。</p>
<h2 id="将复杂的-Swift-代码转换为-LLDB-命令"><a href="#将复杂的-Swift-代码转换为-LLDB-命令" class="headerlink" title="将复杂的 Swift 代码转换为 LLDB 命令"></a>将复杂的 Swift 代码转换为 LLDB 命令</h2><p>通过前面的介绍，我们知道添加自定义 Swift 代码作为 LLDB 命令，必须在一行中完成。因此，如果有一个多行的 Swift 函数，我们必须先将其转换为单行，然后才能将其添加到 <code>.lldbinit</code> 文件中。</p>
<p>比如要添加下面这个将 RGB 值转换为十六进制值的 Swift 函数：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">hex</span><span class="params">(r: Int, g: Int, b: Int)</span></span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* Make sure RGB value within range */</span></span><br><span class="line">    <span class="keyword">if</span> (r &gt;= <span class="number">0</span> &amp;&amp; r &lt;= <span class="number">255</span>) &amp;&amp; (g &gt;= <span class="number">0</span> &amp;&amp; g &lt;= <span class="number">255</span>) &amp;&amp; (b &gt;= <span class="number">0</span> &amp;&amp; b &lt;= <span class="number">255</span>) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> rgb:<span class="type">Int</span> = r&lt;&lt;<span class="number">16</span> | g&lt;&lt;<span class="number">8</span> | b&lt;&lt;<span class="number">0</span></span><br><span class="line">        <span class="keyword">let</span> hex = <span class="type">String</span>(format:<span class="string">"#%06x"</span>, rgb)</span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(hex)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">"Invalid input value"</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>注意：</strong> 上面的代码注释使用的是 <code>/* */</code> 而不是 <code>//</code>,这是为了确保将 Swift 代码转换为单行后不会中断。</p>
<p>下面我们需要将 Swift 代码转换为一行，操作流程如下：</p>
<ol>
<li>为每个函数参数定义一个变量。</li>
<li>将 <code>%1</code>、<code>%2</code>、<code>%3</code>… 分配给每个定义的变量。</li>
<li>在每个语句的末尾添加 <code>;</code>。</li>
</ol>
<p>更新后的 Swift 代码如下：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> r = %<span class="number">1</span>;</span><br><span class="line"><span class="keyword">let</span> g = %<span class="number">2</span>;</span><br><span class="line"><span class="keyword">let</span> b = %<span class="number">3</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* Make sure RGB value within range */</span></span><br><span class="line"><span class="keyword">if</span> (r &gt;= <span class="number">0</span> &amp;&amp; r &lt;= <span class="number">255</span>) &amp;&amp; (g &gt;= <span class="number">0</span> &amp;&amp; g &lt;= <span class="number">255</span>) &amp;&amp; (b &gt;= <span class="number">0</span> &amp;&amp; b &lt;= <span class="number">255</span>) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> rgb:<span class="type">Int</span> = r&lt;&lt;<span class="number">16</span> | g&lt;&lt;<span class="number">8</span> | b&lt;&lt;<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">let</span> hex = <span class="type">String</span>(format:<span class="string">"#%06x"</span>, rgb);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(hex);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">"Invalid input value"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>下面，我们需要将 Swift 代码转换为一行，我比较喜欢使用这个<a href="https://www.textfixer.com/tools/paragraph-to-lines.php" title="代码单行转换工具" target="_blank" rel="noopener">免费在线工具</a>进行单行转换</p>
<p>将代码转换为单行后，就可以进行构造正则表达式命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">command regex hex &apos;s/(.+) (.+) (.+)/expr -l Swift -O -- let r = %1; let g = %2; let b = %3; if (r &gt;= 0 &amp;&amp; r &lt;= 255) &amp;&amp; (g &gt;= 0 &amp;&amp; g &lt;= 255) &amp;&amp; (b &gt;= 0 &amp;&amp; b &lt;= 255) &#123; let rgb:Int = r&lt;&lt;16 | g&lt;&lt;8 | b&lt;&lt;0; let hex = String(format:&quot;#%06x&quot;, rgb); print(hex); &#125; else &#123; print(&quot;Invalid input value&quot;); &#125;/&apos;</span><br></pre></td></tr></table></figure>
<p>将命令粘贴到 <code>.lldbinit</code> 文件中，然后就可以使用了。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/2829694-28714c53ef002c05.gif?imageMogr2/auto-orient/strip" alt=""></p>
<h2 id="实用的自定义-LLDB-命令"><a href="#实用的自定义-LLDB-命令" class="headerlink" title="实用的自定义 LLDB 命令"></a>实用的自定义 LLDB 命令</h2><p>在学会了如何向 LLDB 调试器中添加自定义命令，那么添加什么样的自定义 LLDB 命令最实用呢？</p>
<p>我个人认为下面的自定义命令非常实用。可以在 Xcode 控制台中将任何 JSON 可序列化类型（例如字典、数组、数据等）打印为 JSON 字符串。 可以参考<a href="https://soffes.blog/debugging-json-data-in-lldb" title="Debugging JSON Data in LLDB" target="_blank" rel="noopener">这篇文章</a>。</p>
<p>另外，我也很喜欢<a href="https://diamantidis.github.io/2018/10/14/modifying-ui-elements-with-xcode-and-lldb-v2" title="Modifying UI elements with Xcode and LLDB v2" target="_blank" rel="noopener">本文</a>讨论的一系列自定义命令，我们可以使用它们来动态修改 UI 元素的颜色，而无需重新构建项目。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本文只是对 LLDB 调试器功能做了简单介绍。如果你是刚刚接触到 LLDB，希望这篇文章能帮助你开始探索这个神奇的调试工具。</p>
<p>本文已在公众号「<strong>Swift 社区</strong>」发布，如需转载请加微信：fzhanfei，备注转载开白</p>

      
    </div>
    
    
    
    

    
      <div>
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      </div>
    


    
      <div>
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/如何将-Swift-代码添加为自定义-LLDB-命令/">如何将 Swift 代码添加为自定义 LLDB 命令</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 Swift社区 的个人博客">Swift社区</a></p>
  <p><span>发布时间:</span>2021年06月07日 - 20:06</p>
  <p><span>最后更新:</span>2021年06月07日 - 21:06</p>
  <p><span>原始链接:</span><a href="/如何将-Swift-代码添加为自定义-LLDB-命令/" title="如何将 Swift 代码添加为自定义 LLDB 命令">https://fanbaoying.github.io/如何将-Swift-代码添加为自定义-LLDB-命令/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://fanbaoying.github.io/如何将-Swift-代码添加为自定义-LLDB-命令/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
        });
    });  
</script>

      </div>
    


    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="Swift社区 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="Swift社区 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/iOS/" rel="tag"><i class="fa fa-tag"></i> iOS</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/探索-SwiftUI-基本手势-md/" rel="next" title="探索 SwiftUI 基本手势.md">
                <i class="fa fa-chevron-left"></i> 探索 SwiftUI 基本手势.md
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/Swift-周报-第一期/" rel="prev" title="Swift 周报 第一期">
                Swift 周报 第一期 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://avatars.githubusercontent.com/u/84354365?v=4"
                alt="Swift社区" />
            
              <p class="site-author-name" itemprop="name">Swift社区</p>
              <p class="site-description motion-element" itemprop="description">我们的使命是做一个最专业最权威的 Swift 中文社区，我们的愿景是希望更多的人学习和使用Swift。我们会分享以 Swift 实战、SwiftUI、Swift 基础为核心的技术干货，不忘初心，牢记使命。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">187</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36478920" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a30d987f265da430d580126" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-spinner"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/fanbaoying" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/swift-community" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/7711465033" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAxNzgzNTgwMw==&action=getalbum&album_id=2024314764775981057" target="_blank" title="公众号">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>公众号</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftWeekly" title="SwiftWeekly" target="_blank">SwiftWeekly</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/article-ios" title="Article-ios" target="_blank">Article-ios</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftUI-Book" title="SwiftUI-Book" target="_blank">SwiftUI-Book</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-前言"><span class="nav-number">1.</span> <span class="nav-text">1. 前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-添加您的第一个-LLDB-命令"><span class="nav-number">2.</span> <span class="nav-text">2. 添加您的第一个 LLDB 命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-了解-LLDB-命令结构"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 了解 LLDB 命令结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-添加自定义命令"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 添加自定义命令</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-添加带参数的-LLDB-命令"><span class="nav-number">3.</span> <span class="nav-text">3. 添加带参数的 LLDB 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#将复杂的-Swift-代码转换为-LLDB-命令"><span class="nav-number">4.</span> <span class="nav-text">将复杂的 Swift 代码转换为 LLDB 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实用的自定义-LLDB-命令"><span class="nav-number">5.</span> <span class="nav-text">实用的自定义 LLDB 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">6.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Swift社区</span>

  
</div>

<!-- 
<div class="powered-by">
  <i class="fa fa-user-md">
  </i>
  <span id="busuanzi_container_site_uv">
    已有<span id="busuanzi_value_site_uv"></span>人来访
  </span>

</div>


  <span class="post-meta-divider">|</span>


<div class="powered-by">
  <i class="fa fa-eye"></i>
  <span id="busuanzi_container_site_pv">
      访问量<span id="busuanzi_value_site_pv"></span>次
  </span>
</div>



  <span class="post-meta-divider">|</span>


-->

<div class="theme-info">
  <i class="fa fa-pencil"></i>
  <span class="post-count">博客全站共361.6k字</span>
</div>


<!-- 
  <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>



-->

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
